Page({
  data: {
    tabs: [
      { id: 0, name: '全部' },
      { id: 1, name: '待付款' },
      { id: 2, name: '待确认' },
      { id: 3, name: '待评价' },
      { id: 4, name: '已完成' }
    ],
    currentTab: 0,
    orderList: []
  },

  onLoad(options) {
    // 如果有传入type参数，切换到对应tab
    const { type } = options
    if (type) {
      this.setData({ currentTab: Number(type) })
    }
    this.getOrderList()
  },

  // 切换标签
  onTabChange(e) {
    const { index } = e.currentTarget.dataset
    this.setData({ 
      currentTab: index,
      orderList: [] // 清空列表，避免切换时显示旧数据
    })
    this.getOrderList()
  },

  // 获取订单列表
  getOrderList() {
    // TODO: 调用获取订单列表API
    // 这里模拟一些数据
    setTimeout(() => {
      this.setData({
        orderList: [
          {
            id: 1,
            typeName: '房屋租赁',
            statusText: '待付款',
            statusClass: 'status-unpaid',
            imageUrl: '/assets/images/house1.jpg',
            title: '阳光小区 2室1厅',
            description: '租期：2024-03-01 至 2025-02-28',
            amount: 3500,
            createTime: '2024-03-01 14:30',
            canCancel: true,
            canPay: true
          },
          {
            id: 2,
            typeName: '停车服务',
            statusText: '待确认',
            statusClass: 'status-processing',
            imageUrl: '/assets/images/parking1.jpg',
            title: '中心广场停车场',
            description: '包月服务：2024-03',
            amount: 300,
            createTime: '2024-03-01 10:20',
            canConfirm: true
          }
        ]
      })
    }, 500)
  },

  // 下拉刷新
  onRefresh() {
    this.getOrderList()
  },

  // 查看订单详情
  onOrderDetail(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/order/detail?id=${id}`
    })
  },

  // 取消订单
  onCancelOrder(e) {
    const { id } = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: '确定要取消该订单吗？',
      success: (res) => {
        if (res.confirm) {
          // TODO: 调用取消订单API
          wx.showToast({
            title: '取消成功',
            icon: 'success'
          })
          this.getOrderList()
        }
      }
    })
  },

  // 支付订单
  onPayOrder(e) {
    const { id } = e.currentTarget.dataset
    // TODO: 调用支付API
    wx.showLoading({
      title: '支付中'
    })
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '支付成功',
        icon: 'success'
      })
      this.getOrderList()
    }, 1500)
  },

  // 确认订单
  onConfirmOrder(e) {
    const { id } = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: '确认已完成该服务？',
      success: (res) => {
        if (res.confirm) {
          // TODO: 调用确认订单API
          wx.showToast({
            title: '确认成功',
            icon: 'success'
          })
          this.getOrderList()
        }
      }
    })
  },

  // 评价订单
  onRateOrder(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/order/rate?id=${id}`
    })
  },

  // 返回上一页
  onBack() {
    wx.navigateBack({
      delta: 1
    })
  }
}) 